﻿#include "function_find_shape_model.h"



// Procedures 
void findShapeModel(HObject ho_image, HObject *ho_xld_show, HTuple hv_model, HTuple *hv_score,
	HTuple *hv_ret)
{

	// Local iconic variables
	HObject  ho_ObjectSelected;

	// Local control variables
	HTuple  hv_ModelRow, hv_ModelColumn, hv_ModelAngle;
	HTuple  hv_HomMat2D;

	FindShapeModel(ho_image, hv_model, 0, 6.29, 0.3, 1, 0.5, "least_squares", 0,
		0.9, &hv_ModelRow, &hv_ModelColumn, &hv_ModelAngle, &(*hv_score));
	
	if (0 != (int(((*hv_score).TupleLength()) == 0)))
	{
		(*hv_ret) = -1;
		return;
	}
	GetShapeModelContours(&(*ho_xld_show), hv_model, 1);
	VectorAngleToRigid(0, 0, 0, hv_ModelRow, hv_ModelColumn, hv_ModelAngle, &hv_HomMat2D);
	AffineTransContourXld((*ho_xld_show), &(*ho_xld_show), hv_HomMat2D);


	SelectObj((*ho_xld_show), &ho_ObjectSelected, 1);

	(*hv_ret) = 1;

}


FunctionFindShapeModel::FunctionFindShapeModel(QDialog* parent, QSettings* qsettings, QString section,QString dir) : QDialog(parent)
{
    m_qsettings = qsettings;

    m_section = section;

    m_create_shape_model = new FunctionCreateShapeModel(this, dir, section);

    this->setWindowTitle(("find shape model"));
    QRect desktop_rect = QApplication::desktop()->geometry();
    double ratio_x = desktop_rect.width()/1920.0;
    double ratio_y = desktop_rect.height()/1080.0;

    move(0,0);
    this->setFixedSize(1920*ratio_x,1010*ratio_y);

    QHBoxLayout* hlayout =new QHBoxLayout();
    this->setLayout(hlayout);



    QFrame* p = new QFrame(this);
    QSize control_size(240*ratio_x,40*ratio_y);
    p->setFrameShape(QFrame::WinPanel);
    p->move(0*ratio_x,0*ratio_y);
    p->setFixedSize(600*ratio_x,800*ratio_y);

    hlayout->addWidget(p);

    QGridLayout *qgridLayout = new QGridLayout();
    p->setLayout(qgridLayout);

    int index = 0;

    QSize ButtonSize(220 * ratio_x, 40 * ratio_y);


    m_qlabel_score_standard = new QLabel();
    m_qlabel_score_standard->setText("Match Score");
    m_qlabel_score_standard->setFixedSize(control_size);
    qgridLayout->addWidget(m_qlabel_score_standard,index,0);
    m_qdoublespinbox_score_standard = new QDoubleSpinBox();
    m_qdoublespinbox_score_standard->setFixedSize(control_size);
    m_qdoublespinbox_score_standard->setMinimum(0);
    m_qdoublespinbox_score_standard->setMaximum(1);
    qgridLayout->addWidget(m_qdoublespinbox_score_standard, index, 1);
    index++;

   /* m_pushbutton_new_model = new QPushButton(this);
    m_pushbutton_new_model->setFixedSize(control_size);
    m_pushbutton_new_model->setText("Create New Model");
    connect(m_pushbutton_new_model, &QPushButton::clicked, this, [=]() {
		m_create_shape_model->m_image = m_image;
		m_create_shape_model->showNew();
        return true;
    });
    qgridLayout->addWidget(m_pushbutton_new_model, index, 0);*/

    m_pushbutton_edit_model = new QPushButton(this);
    m_pushbutton_edit_model->setFixedSize(control_size);
    m_pushbutton_edit_model->setText("Edit Model");
    connect(m_pushbutton_edit_model, &QPushButton::clicked, this, [=]() {
		m_create_shape_model->m_image_current = m_image;
        m_create_shape_model->showEdit();

        return true;
    });
    qgridLayout->addWidget(m_pushbutton_edit_model, index, 0);
    index++;

    m_pushbutton_show_orign_image = new QPushButton(this);
    m_pushbutton_show_orign_image->setFixedSize(control_size);
    m_pushbutton_show_orign_image->setText("Show Orign Image");
    connect(m_pushbutton_show_orign_image, &QPushButton::clicked, this, [=]() {
        QImage q_image = matToQImage(m_image);
        m_view->setImage(q_image, "");
        return true;
    });
    qgridLayout->addWidget(m_pushbutton_show_orign_image, index, 0);
	m_pushbutton_detection = new QPushButton(this);
	m_pushbutton_detection->setFixedSize(control_size);
    m_pushbutton_detection->setText("Detection");
    connect(m_pushbutton_detection, &QPushButton::clicked, this, &FunctionFindShapeModel::detectionImage);
    qgridLayout->addWidget(m_pushbutton_detection, index, 1);
    index++;

    m_pushbutton_save_parameter = new QPushButton(this);
    m_pushbutton_save_parameter->setFixedSize(control_size);
    m_pushbutton_save_parameter->setText("Save");
    connect(m_pushbutton_save_parameter, &QPushButton::clicked, this, &FunctionFindShapeModel::saveParameter);
    qgridLayout->addWidget(m_pushbutton_save_parameter, index,0);

    m_pushbutton_return = new QPushButton(this);
    m_pushbutton_return->setFixedSize(control_size);
    m_pushbutton_return->setText("Exit");
    qgridLayout->addWidget(m_pushbutton_return, index, 1);
    connect(m_pushbutton_return, &QPushButton::clicked, this, [=]() {
        this->hide();
        });



    {//加入图像显示
        QVBoxLayout* view_layout =new QVBoxLayout();
        view_layout->setMargin(0);
        m_view = new QtQGraphicsView(this);
        m_view->setFixedSize(1250 * ratio_x, 900 * ratio_y);
        connect(m_view->m_pixmapItem, &QtQGraphicsPixmapItem::sendMessage, this,[=](QPointF pos,QString message)
        {
            Q_UNUSED(pos);
            m_view_pixel_qlabel->setText(message);
        });
        view_layout->addWidget(m_view);
        m_view_pixel_qlabel = new QLabel(this);
        m_view_pixel_qlabel->setAlignment(Qt::AlignRight );
        m_view_pixel_qlabel->setText("");
        m_view_pixel_qlabel->setFixedSize(1250 * ratio_x, 20 * ratio_y);
        view_layout->addWidget(m_view_pixel_qlabel);
        hlayout->addLayout(view_layout);
    }
	loadParameter();
}

FunctionFindShapeModel::~FunctionFindShapeModel()
{
    ;
}

bool FunctionFindShapeModel::detection( HObject image_in, QString& message)
{
    try
    {
		if (m_create_shape_model->m_model_match.Length() == 0)
		{
			message = "no model";
			return false;
		}

        HTuple hv_ret, hv_max_value;
        findShapeModel(image_in,&m_xld_show, m_create_shape_model->m_model_match,&hv_max_value,&hv_ret);


        int ret = hv_ret[0].I();
		
        if(ret>0)
        {
			double score = hv_max_value[0].D();

			
			if (score > m_score_standard)
				return true;
			else
			{
				message = QString::number(score);
				return false;
			}
        }
        else
        {
			message = "no find";
            return false;
        }
    }
	catch (HException &HDevExpDefaultException)
	{
		message = HDevExpDefaultException.ErrorMessage();
		return false;
	}
  
    return true;
}

void FunctionFindShapeModel::detectionImage()
{
    if(m_image.empty())
    {
        QMessageBox::information(nullptr,"","no image");
        return;
    }

    QString message;
    HObject image = matToHImage(m_image);
    bool state = detection(image, message);


    if(state)
		message += "OK";
	else
		message += "NG";
    Mat image_show = m_image.clone();
    draw(image_show, image_show);

    QImage q_image = matToQImage(image_show);

    m_view->setImage(q_image, message);
    
    }

void FunctionFindShapeModel::draw(Mat image_in,Mat& image_out)
{
    if(image_in.empty())
    {
        return;
    }
	if (image_in.channels() == 1)
	{
		cvtColor(image_in, image_in, COLOR_GRAY2BGR);
	}


    image_out = drawXld(image_in, m_xld_show, Scalar(0, 255,0));
	m_xld_show.Clear();
}

void FunctionFindShapeModel::loadParameter()
{
	m_score_standard = m_qsettings->value(m_section + "/" + "m_score_standard").toDouble();

	m_qdoublespinbox_score_standard->setValue(m_score_standard);
    return ;
}

void FunctionFindShapeModel::saveParameter()
{
	m_score_standard = m_qdoublespinbox_score_standard->value();
	m_qsettings->setValue(m_section + "/" + "m_score_standard", m_score_standard);
    return;
}

bool FunctionFindShapeModel::showDialog()
{
    QImage q_image = matToQImage(m_image);
    m_view->setImage(q_image, "");
   
    return true;
}



